package com.kanesoft.android.gdx.actors.fight;

import com.badlogic.gdx.math.Vector2;
import com.kanesoft.android.gdx.actors.ActorPhysicsParameters;
import com.kanesoft.android.gdx.widgets.sprites.SpriteInfo;

public class FighterWalkingForward extends FightActorStateMachine {

    private FightActorObject fightActor;

    public FighterWalkingForward(SpriteInfo stateSpriteInfo, FightActorObject actor) {
        super(stateSpriteInfo, actor);
        fightActor = actor;
    }

    @Override
    public boolean OnEnter() {
        
        if (fightActor.getActorState() != null) {
            if (fightActor.getActorState().equals(fightActor.waitingState) == false) return false;
        }
        
        actorSprite.setAnimationInLoop(false);
        actorSprite.setSpriteInfo(stateSpriteInfo);

        return true;
    }

    @Override
    public boolean OnUpdate() {

        if (actor.getWorldControl().isObjectOnTheGround(actor) == false) {
            actor.setActorState(fightActor.fallingState);
            return false;
        }

        fightActor.getSensorFixture().setFriction(0.2f);

        Vector2 objVelocity = fightActor.getActorBody().getLinearVelocity();
        Vector2 objPosition = fightActor.getActorBody().getPosition();
        ActorPhysicsParameters objectPhysicsParams = fightActor.getPhysicsParameters();

        if (Math.abs(objVelocity.x) < objectPhysicsParams.bodyMaxSpeed) {

            float signDirection = 1;

            if (fightActor.getHorizontalFlip()) signDirection = -1;

            fightActor.getActorBody().applyLinearImpulse(signDirection * objectPhysicsParams.bodyWalkIncreaseSpeed, 0,
                    objPosition.x, objPosition.y);

        } else {
            fightActor.getActorBody().setLinearVelocity(Math.signum(objVelocity.x) * objectPhysicsParams.bodyMaxSpeed,
                    objVelocity.y);
        }

        return true;
    }

    @Override
    public String getStateName() {
        return "WalkingForward";
    }

}
